<template>
  <div class="app-container">
    <el-form  class="formWidth" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <!-- <el-form-item label="退役军人ID" prop="soldierId">
        <el-input
          v-model="queryParams.soldierId"
          placeholder="请输入退役军人ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable size="small">
          <el-option
            v-for="dict in sexOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="phoneNo">
        <el-input
          v-model="queryParams.phoneNo"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="头像" prop="avatar">
        <el-input
          v-model="queryParams.avatar"
          placeholder="请输入头像"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="出生年份" prop="yearOfBirth">
        <el-date-picker clearable size="small"
          :picker-options="pickerOptions"
          v-model="queryParams.yearOfBirth"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择出生年份">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="工作经验" prop="workExperience">
        <el-select v-model="queryParams.workExperience" placeholder="请选择工作经验" clearable size="small">
          <el-option
            v-for="dict in workExperienceOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="最高学历" prop="education">
        <el-select v-model="queryParams.education" placeholder="请选择最高学历" clearable size="small">
          <el-option
            v-for="dict in educationOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="职业标签" prop="occupationalLabel">
        <el-input
          v-model="queryParams.occupationalLabel"
          placeholder="请输入职业标签"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="求职岗位" prop="expectedCareerType">
        <el-select v-model="queryParams.expectedCareerType" placeholder="请选择求职岗位" clearable size="small">
          <el-option
            v-for="dict in expectedCareerTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['enterprise:resume:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['enterprise:resume:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['enterprise:resume:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['enterprise:resume:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="resumeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="简历ID" align="center" prop="resumeId" /> -->
      <!-- <el-table-column label="退役军人ID" align="center" prop="soldierId" /> -->
      <el-table-column label="姓名" show-overflow-tooltip align="center" prop="username" />
      <el-table-column label="性别" show-overflow-tooltip align="center" prop="sex" :formatter="sexFormat" />
      <el-table-column label="手机号"  show-overflow-tooltip align="center" prop="phoneNo" />
      <el-table-column label="出生年份" show-overflow-tooltip align="center" prop="yearOfBirth" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.yearOfBirth, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工作经验" show-overflow-tooltip align="center" prop="workExperience" :formatter="workExperienceFormat" />
      <el-table-column label="最高学历" show-overflow-tooltip align="center" prop="education" :formatter="educationFormat" />
      <el-table-column label="职业标签" show-overflow-tooltip  align="center" prop="occupationalLabel" />
      <el-table-column label="求职岗位" show-overflow-tooltip align="center" prop="expectedCareerType" :formatter="expectedCareerTypeFormat" />
      <!-- <el-table-column label="头像" show-overflow-tooltip align="center" prop="avatar" /> -->
      <el-table-column align="center" width="80" prop="url" label="头像">
          <template slot-scope="scope">
            <el-popover placement="right" title="" trigger="hover">
              <img :src="newUrl(scope.row.avatar)" style="height: 200px;width: 200px">
              <img slot="reference" :src="newUrl(scope.row.avatar)" :alt="newUrl(scope.row.avatar)" style="max-height: 50px;max-width: 130px">
            </el-popover>
          </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['enterprise:resume:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['enterprise:resume:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改简历信息对话框 -->
    <el-dialog class="addDia" :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form class="formWidth" ref="form" :model="form" :rules="rules" label-width="100px">
        <!-- <el-form-item label="退役军人ID" prop="soldierId">
          <el-input v-model="form.soldierId" placeholder="请输入退役军人ID" />
        </el-form-item> -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
           <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别">
                <el-option
                  v-for="dict in sexOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>            
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phoneNo">
              <el-input v-model="form.phoneNo" placeholder="请输入手机号" />
            </el-form-item>
          </el-col>
           <el-col :span="12">
            <el-form-item label="出生年份" prop="yearOfBirth">
              <el-date-picker clearable size="small"
                :picker-options="pickerOptions"
                v-model="form.yearOfBirth"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择出生年份">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工作经验" prop="workExperience">
              <el-select v-model="form.workExperience" placeholder="请选择工作经验">
                <el-option
                  v-for="dict in workExperienceOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最高学历" prop="education">
              <el-select v-model="form.education" placeholder="请选择最高学历">
                <el-option
                  v-for="dict in educationOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职业标签" prop="occupationalLabel">
              <el-input v-model="form.occupationalLabel" placeholder="请输入职业标签" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="求职岗位" prop="expectedCareerType">
              <el-select v-model="form.expectedCareerType" placeholder="请选择求职岗位">
                <el-option
                  v-for="dict in expectedCareerTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="头像" prop="avatar">
              <!-- <el-input v-model="form.avatar" type="textarea" placeholder="请输入头像" /> -->
              <ResumeAvatar :resume.sync="form" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ResumeAvatar from "./ResumeAvatar";
import { listResume, getResume, delResume, addResume, updateResume, exportResume } from "@/api/enterprise/resume";

export default {
  name: "Resume",
  components: { ResumeAvatar },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 简历信息表格数据
      resumeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 性别字典
      sexOptions: [],
      // 工作经验字典
      workExperienceOptions: [],
      // 最高学历字典
      educationOptions: [],
      // 求职岗位字典
      expectedCareerTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        soldierId: null,
        username: null,
        sex: null,
        phoneNo: null,
        avatar: null,
        yearOfBirth: null,
        workExperience: null,
        education: null,
        occupationalLabel: null,
        expectedCareerType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        username: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        phoneNo: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        yearOfBirth: [
          { required: true, message: "出生年份不能为空", trigger: "blur" }
        ],
        workExperience: [
          { required: true, message: "工作经验不能为空", trigger: "change" }
        ],
        education: [
          { required: true, message: "最高学历不能为空", trigger: "change" }
        ],
        expectedCareerType: [
          { required: true, message: "求职岗位不能为空", trigger: "change" }
        ],
      },
      pickerOptions:{
         disabledDate(time) {
            return time.getTime() > Date.now() - 8.64e6;//如果没有后面的-8.64e6就是不可以选择今天的
          }
      },
    };
  },
  created() {
    this.reset()
    this.getList()
    this.getDicts("s_sex").then(response => {
      this.sexOptions = response.data;
    });
    this.getDicts("s_work_experience").then(response => {
      this.workExperienceOptions = response.data;
    });
    this.getDicts("s_education").then(response => {
      this.educationOptions = response.data;
    });
    this.getDicts("s_job_list").then(response => {
      this.expectedCareerTypeOptions = response.data;
    });
  },
  methods: {
    newUrl(url){
      return process.env.VUE_APP_BASE_API + url
    },
    /** 查询简历信息列表 */
    getList() {
      this.loading = true;
      listResume(this.queryParams).then(response => {
        this.resumeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 性别字典翻译
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex);
    },
    // 工作经验字典翻译
    workExperienceFormat(row, column) {
      return this.selectDictLabel(this.workExperienceOptions, row.workExperience);
    },
    // 最高学历字典翻译
    educationFormat(row, column) {
      return this.selectDictLabel(this.educationOptions, row.education);
    },
    // 求职岗位字典翻译
    expectedCareerTypeFormat(row, column) {
      return this.selectDictLabel(this.expectedCareerTypeOptions, row.expectedCareerType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        resumeId: null,
        soldierId: null,
        username: null,
        sex: null,
        phoneNo: null,
        avatar: null,
        yearOfBirth: null,
        workExperience: null,
        education: null,
        occupationalLabel: null,
        expectedCareerType: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.resumeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加简历信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const resumeId = row.resumeId || this.ids
      getResume(resumeId).then(response => {
        this.form = response.data;
        // this.$set(this.form, 'sex',String(response.data.sex))
        this.open = true;
        this.title = "修改简历信息";
      });
    },
    /** 根据勾选生日日期计算周岁年龄 */
    setAge(info){
      var yearOfBirth = info.yearOfBirth
      var birthdays = new Date(yearOfBirth.replace(/-/g, "-"));
      var d = new Date();
      var age =
        d.getFullYear() -
        birthdays.getFullYear() -
        (d.getMonth() < birthdays.getMonth() ||
        (d.getMonth() == birthdays.getMonth() &&
          d.getDate() < birthdays.getDate())
          ? 1
          : 0);
      info.age = age;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.setAge(this.form)
          if (this.form.resumeId != null) {
            updateResume(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addResume(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const resumeIds = row.resumeId || this.ids;
      // this.$confirm('是否确认删除简历信息编号为"' + resumeIds + '"的数据项?', "警告", {
        this.$confirm('是否确认删除该项数据?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delResume(resumeIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有简历信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportResume(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
<style scoped>
.formWidth >>> .el-textarea{
  width: 220px !important;
}
</style>